package com.bootx.controller.api;

import com.bootx.common.Result;
import com.bootx.controller.BaseController;
import com.bootx.entity.BaseEntity;
import com.bootx.service.ArticleService;
import com.fasterxml.jackson.annotation.JsonView;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/article")
public class ArticleController extends BaseController {

    @Resource
    private ArticleService articleService;

    @PostMapping("/ai")
    @JsonView({BaseEntity.TreeView.class})
    public Result ai(){
        List<Map<String, Object>> list = new ArrayList<>();
        List<Map<String, Object>> maps = jdbcTemplate.queryForList("select date, 'date' `type` from article group by date order by date desc");
        maps.forEach(map -> {
            list.add(map);
            list.addAll(jdbcTemplate.queryForList("select id,memo,title,source,url,'news' `type` from article where date=?",map.get("date")));
        });
        return Result.success(list);
    }

    @PostMapping("/detail")
    @JsonView({BaseEntity.ViewView.class})
    public Result detail(Long id){
        return Result.success(articleService.find(id));
    }

}
